<template>
  <!--  <el-dialog-->
  <!--    :close-on-click-modal="false"-->
  <!--    :title="title"-->
  <!--    :visible.sync="dialogFormVisible"-->
  <!--    append-to-body-->
  <!--    width="800px"-->
  <!--    @close="close"-->
  <!--  >-->
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="105px">
      <el-row :gutter="10">

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="店铺名称" prop="title">
            <el-input v-model="form.title" autocomplete="off" placeholder="店铺名称"></el-input>
            <div class="el-form-item__error miaosu">
              店铺名称
            </div>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="经纬度" prop="logLat">
            <el-input v-model="form.logLat" autocomplete="off" placeholder="东经,北纬"></el-input>
            <div class="el-form-item__error miaosu">
              东经,北纬 例如 116.404,39.915 <a href="http://api.map.baidu.com/lbsapi/getpoint/index.html">点击我</a> 获取
            </div>
          </el-form-item>

        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="图片" prop="images">
            <UploadImage ref="UploadImage" :imgageUrl="form.images" @returnImgUrl="returnImgUrl"></UploadImage>
            <div class="el-form-item__error miaosu">
              第一张图为封面图
            </div>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>


        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="标签" prop="tag">
            <el-input v-model="form.tag" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="描述" prop="describe">
            <el-input v-model="form.describes" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">
          <el-form-item label="wifi" prop="wifi">
            <el-input v-model="form.wifi" autocomplete="off"></el-input>
          </el-form-item>
        </el-col>

        <!--        private String logLat;-->

        <!--        private String images;-->

        <!--        private String address;-->

        <!--        private String phone;-->

        <!--        private String tag;-->

        <!--        private String title;-->

        <!--        private String describe;-->

        <!--        private String wifi;-->

        <!--        <div>-->
        <!--          <el-col :lg="12" :md="24" :sm="24" :xl="12" :xs="24">-->
        <!--            <el-form-item label="名称" prop="name">-->
        <!--              <el-input v-model="form.title" autocomplete="off"></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->


        <!--          <el-col :lg="6" :md="12" :sm="12" :xl="6" :xs="12">-->
        <!--            <el-form-item label="是否默认" prop="value">-->
        <!--              <el-switch-->
        <!--                v-model="form.isDefault"-->
        <!--                :active-value="1"-->
        <!--                :disabled="form.isDefault===1&&form.id!=null"-->
        <!--                :inactive-value="0"-->
        <!--                active-color="#13ce66"-->
        <!--                inactive-color="#ff4949">-->
        <!--              </el-switch>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->

        <!--          <el-col :lg="8" :md="12" :sm="12" :xl="8" :xs="12">-->
        <!--            <el-form-item label="参数类型" prop="value">-->
        <!--              <el-select v-model="form.type" :disabled="form.isDefault===1" placeholder="请选择">-->
        <!--                <el-option-->
        <!--                  :value="0"-->
        <!--                  label="文本">-->
        <!--                </el-option>-->
        <!--                <el-option-->
        <!--                  :value="1"-->
        <!--                  label="图片">-->
        <!--                </el-option>-->
        <!--                <el-option-->
        <!--                  :value="3"-->
        <!--                  label="富文本">-->
        <!--                </el-option>-->
        <!--                <el-option-->
        <!--                  :value="4"-->
        <!--                  label="开关">-->
        <!--                </el-option>-->
        <!--                <el-option-->
        <!--                  :value="5"-->
        <!--                  label="数组">-->
        <!--                </el-option>-->
        <!--              </el-select>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->


        <!--          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">-->
        <!--            <el-form-item label="备注" prop="value">-->
        <!--              <el-input v-model="form.remarks" :disabled="form.isDefault===1" autocomplete="off"-->
        <!--                        type="textarea"></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->

        <!--          &lt;!&ndash;    参数类型、0文字，1图片，3富文本，4开关，5文字数组&ndash;&gt;-->

        <!--          <el-col v-if="form.type===0" :lg="24" :md="24" :sm="24" :xl="24" :xs="24">-->
        <!--            <el-form-item label="值" prop="value">-->
        <!--              <el-input v-model="form.value" autocomplete="off" type="textarea"></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->

        <!--          <el-col v-if="form.type===1" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">-->
        <!--            <UploadImage ref="UploadImage" :imgageUrl="form.value" @returnImgUrl="returnImgUrl"></UploadImage>-->
        <!--          </el-col>-->

        <!--          <el-col v-if="form.type===3" :lg="24" :md="24" :sm="24" :xl="24" :xs="24">-->
        <!--            <VabQuill-->
        <!--              ref="QuillEditor"-->
        <!--              v-model="form.value"-->
        <!--              :min-height="300"-->
        <!--              style="width: 100%;"-->
        <!--              @change="onEditorChange($event)"-->
        <!--            ></VabQuill>-->
        <!--          </el-col>-->
        <!--          <el-col v-if="form.type===4" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">-->
        <!--            <el-form-item label="值" prop="value">-->
        <!--              <el-switch-->
        <!--                v-model="form.value"-->
        <!--                :active-value="1"-->
        <!--                :inactive-value="0"-->
        <!--                active-color="#13ce66"-->
        <!--                inactive-color="#ff4949">-->
        <!--              </el-switch>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col v-if="form.type===5" :lg="12" :md="24" :sm="24" :xl="12" :xs="24">-->
        <!--            <div style="display:flex;">-->
        <!--              <el-tag-->
        <!--                v-for="(tag,index) in form.value.split(',')"-->
        <!--                :key="tag"-->
        <!--                :disable-transitions="false"-->
        <!--                closable-->
        <!--                @close="handleClose(index)">-->
        <!--                {{ tag }}-->
        <!--              </el-tag>-->
        <!--              <el-input-->
        <!--                v-if="inputVisible"-->
        <!--                ref="saveTagInput"-->
        <!--                v-model="inputValue"-->
        <!--                class="input-new-tag"-->
        <!--                size="small"-->
        <!--                @blur="handleInputConfirm"-->
        <!--                @keyup.enter.native="handleInputConfirm"-->
        <!--              >-->
        <!--              </el-input>-->
        <!--              <el-button v-else class="button-new-tag" size="mini" style="margin-left: 10px" @click="showInput">加一个-->
        <!--              </el-button>-->
        <!--            </div>-->
        <!--          </el-col>-->
        <!--        </div>-->
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </div>

  <!--  </el-dialog>-->
</template>

<script>
import {doInsert, doUpdate} from "@/api/shop/shopconfig/ShopConfigManagementApi";
import {isNull} from "@/utils/validate";
import {formateDate} from "@/utils/format";
import UploadImage from "@/views/modules/common/UploadImage";
import VabQuill from 'zx-quill'
import 'zx-quill/dist/zx-quill.css'
import {uploadJsonFile} from "@/api/commom";

export default {
  components: {UploadImage, VabQuill},
  name: "ShopConfigManagementEdit",
  data() {

    return {
      options: null,
      inputVisible: false,
      inputValue: '',
      form: {
        // 设置默认值
        type: 0,
        version: 0
      },
      baseForm: {
        locked: "0"
      },

      dict: {},
      rules: {
        logLat: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], images: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], address: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], phone: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], tag: [
          {required: true, trigger: "blur", message: "必须填写"},
        ], title: [
          {required: true, trigger: "blur", message: "必须填写"},
        ]
      },
      title: "",
      dialogFormVisible: false,
    };
  },
  created() {

  },
  mounted() {
    // 加载字典值
  },
  methods: {

    handleClose(index) {
      // console.log(index)
      let array = this.form.value.split(',')
      array.splice(index, 1);
      // console.log(array)
      this.form.value = array.join();
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        if (isNull(this.form.value)) {
          this.form.value = inputValue;
        } else {
          this.form.value += ',' + inputValue;
        }
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    /**
     * 返回的图片地址
     * @param e
     */
    returnImgUrl(e) {
      // console.log(e)
      this.form.images = e || ""
    },

    showEdit(row) {
      if (!row) {
        this.title = "添加";
      } else {
        this.title = "编辑";
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.dialogFormVisible = false;
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
    },
    async save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 处理数据
          this.handlerFormData(this.form);

          if (!isNull(this.form.type) && this.form.type === 3) {
            let {data} = await uploadJsonFile("json", this.form.value);
            this.form.value = data.fileStoragePath

          }

          // 修改
          if (!isNull(this.form.id)) {
            const {msg} = await doUpdate(this.form);
            this.$baseMessage(msg, "success");
          } else {
            const {msg} = await doInsert(this.form);
            this.$baseMessage(msg, "success");
          }

          await this.$emit("fetchData");
          this.close();
        } else {
          return false;
        }
      });
    },
    // 处理 form数据
    handlerFormData(formData) {
      if (!isNull(formData)) {
        for (let key in formData) {
          // 对于时间类进行处理
          if ("[object Date]" === Object.prototype.toString.call(formData[key])) {
            formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
          }
        }
      }
    },
  },
};
</script>

<style scoped>
.miaosu {
  color: #999;
  right: 10px;
  left: auto;
}
</style>
